package com.hnucm.cooklife.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hnucm.cooklife.R;
import com.hnucm.cooklife.bean.db.Dish;
import com.hnucm.cooklife.bean.db.SearchResult;
import com.hnucm.cooklife.bean.me.DishColl;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

public class CollectAdapter extends RecyclerView.Adapter<CollectAdapter.SResultHolder> {

    private Context mContext;
    private List<DishColl> mDishList = new ArrayList<>();

    //此变量类型为自定义接口类型
    private CollectAdapter.onItemClickListener mOnItemClickListener;

    //自定义接口实现点击事件，低耦合
    public interface onItemClickListener{
        void onSeleClickListener(View view,int position);
    }
    public void setOnItemClickListener(CollectAdapter.onItemClickListener listener){
        this.mOnItemClickListener = listener;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setDishList(List<DishColl> dishList) {
        mDishList.clear();
        mDishList.addAll(dishList);
        notifyDataSetChanged();
    }

    public CollectAdapter(Context context, List<DishColl> dishList) {
        mContext = context;
        mDishList.addAll(dishList);
    }

    @NonNull
    @Override
    public SResultHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.search_result_item, parent, false);
        return new SResultHolder(view,mOnItemClickListener);
    }

    @Override
    public void onBindViewHolder(@NonNull SResultHolder holder, int position) {
//        System.out.println(mDishList+"sf");
        if(mDishList != null){
            DishColl dish = mDishList.get(position);
             holder.mSearchName.setText(
                    Html.fromHtml(dish.dishname,Html.FROM_HTML_MODE_COMPACT)
            );

//             holder.mSearchAuthor.setText(
//                     Html.fromHtml(dish.username,Html.FROM_HTML_MODE_COMPACT)
//             );
            Glide.with(mContext)
                    .load(dish.icon)
                    .fitCenter()
                    .centerCrop()
                    .into(holder.mSearchHeader);

            Glide.with(mContext)
                    .load(dish.dishpic)
                    .fitCenter()
                    .centerCrop()
                    .into(holder.mSearchPic);

            holder.mSearchDone.setText(String.format(mContext.getResources().getString(R.string.search_done),dish.dishdone));
            holder.mSearchScore.setText(String.format(mContext.getResources().getString(R.string.search_score),dish.dishscore));
            holder.mSearchAuthor.setText(
                    Html.fromHtml(dish.username,Html.FROM_HTML_MODE_COMPACT)
            );
        }
    }

    @Override
    public int getItemCount() {
        return mDishList.size();
    }

    class SResultHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.search_pic)
        ImageView mSearchPic;

        @BindView(R.id.search_name)
        TextView mSearchName;

        @BindView(R.id.search_score)
        TextView mSearchScore;

        @BindView(R.id.search_done)
        TextView mSearchDone;

        @BindView(R.id.search_header)
        CircleImageView mSearchHeader;

        @BindView(R.id.search_author)
        TextView mSearchAuthor;

        public SResultHolder(@NonNull View itemView,final CollectAdapter.onItemClickListener onItemClickListener) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPosition = getAdapterPosition();
                    if(adapterPosition != RecyclerView.NO_POSITION){
                        onItemClickListener.onSeleClickListener(itemView,adapterPosition);
                    }
                }
            });
        }
    }
}
